var api = new Api();
var isAgree = 1;
var img_btn = $("#img_btn");
img_btn.attr("src", "http://ucenter.ehaoyao.com/showValidateCode.json?" + Math.random());
img_btn.on("click", function () {
	img_btn.attr("src", "http://ucenter.ehaoyao.com/showValidateCode.json?" + Math.random());
});
//输入框获得焦点/失去焦点时，改变icon颜色
$('input').focus(function(){
	$(this).parents('li').removeClass('alert');
	$(this).parents('li').addClass('focus');
});
$('input').blur(function(){
	$(this).parents('li').removeClass('focus');
});
//选择是否同意注册协议
$('#agreement').click(function(){
	$(this).toggleClass('cur');
	if ($(this).hasClass('cur')) {
		isAgree = 1;
	} else {
		isAgree = 0;
	}
});
//显示/隐藏注册协议
$('#showAgreement').click(function(){
	$('.agreement-box').removeClass('hide');
});
$('#hideAgreement').click(function(){
	$('.agreement-box').addClass('hide');
});
$("#username").bind({"focus":function () {
}, "blur":function () {
	var username = $('#username').val();
	var flag = true;
	if (validateRules.isNull(username)) {
		showMsg("username","账号不能为空");
		flag = false;
	} else if(validateRules.isMobile(username)) {
		flag = validatePhone(username);
	} else if(validateRules.isEmail(username)) {
		flag = validateEmail(username);
	} else {
		flag = validateName(username);
	}
	if(flag) {
		$('#usernameTip').addClass('hide');
	}
	if(validateRules.isMobile(username)) {
		$('#verifyCode').parents('li').addClass('hide');
		$('#verifyCodeTip').addClass('hide');
		$('#phoneCode').parents('li').removeClass('hide');
		$('#phoneCodeTip').addClass('hide');
	} else {
		$('#phoneCode').parents('li').addClass('hide');
		$('#phoneCodeTip').addClass('hide');
		$('#verifyCode').parents('li').removeClass('hide');
		$('#verifyCodeTip').addClass('hide');
	}
}});
$("#password").bind({"focus":function () {
}, "blur":function () {
	var username = $('#username').val();
	var password = $('#password').val();
	var flag = true;
	if(validateRules.isMobile(username)) {
		if(!validateRules.isNull(password) && !validateRules.isPwd(password)) {
			showMsg("password","密码为6-20位");
			flag = false;
		}
	} else {
		if (!validateRules.isPwd(password)) {
			showMsg("password","密码为6-20位");
			flag = false;
		}
	}
	if(flag) {
		$('#passwordTip').addClass('hide');
	}
	
}});
$("#confirmPassword").bind({"focus":function () {
}, "blur":function () {
	var username = $('#username').val();
	var password = $('#password').val();
	var confirmPassword = $('#confirmPassword').val();
	var flag = true;
	if(validateRules.isMobile(username)) {
		if(!validateRules.isNull(password) && !validateRules.isPwdRepeat(password, confirmPassword)) {
			showMsg("confirmPassword","两次输入的密码不一致");
			flag = false;
		}
	} else {
		if (!validateRules.isPwdRepeat(password, confirmPassword)) {
			showMsg("confirmPassword","两次输入的密码不一致");
			flag = false;
		}
	}
	if(flag) {
		$('#confirmPasswordTip').addClass('hide');
	}
	
}});
$("#verifyCode").bind({"focus":function () {
}, "blur":function () {
	var verifyCode = $('#verifyCode').val();
	var flag = true;
	if(validateRules.isNull(verifyCode)) {
		showMsg("verifyCode","验证码不能为空");
		flag = false;
	}
	if(flag) {
		$('#verifyCodeTip').addClass('hide');
	}
}});
$("#phoneCode").bind({"focus":function () {
}, "blur":function () {
	var phoneCode = $('#phoneCode').val();
	var flag = true;
	if(validateRules.isNull(phoneCode)) {
		showMsg("phoneCode","手机验证码不能为空");
		flag = false;
	}
	if(flag) {
		$('#phoneCodeTip').addClass('hide');
	}
}});

$('#getDypw').click(function(){
	var flag = false;
	var username = $('#username').val();
	if(validatePhone(username)) {
		var params = {
			"phone" : username
		};
		api.localAjaxForJson(api.localSendRegisterCode, params, function(json) {
			if (json.code != 1) {
				showMsg("phoneCode", json.tip.replace("请求处理失败：",""));
			} else {
				flag = true;
			}
		}, "post");
		if (flag) {
			$("#getDypw").text(120);
			$('#getDypw').addClass("btn-disabled");
			$('#getDypw').attr("disabled","disabled");
			window.setInterval("run();", 1000);
		}
	}
});

$('#register').click(function(){
	var username = $('#username').val();
	var password = $('#password').val();
	var confirmPassword = $('#confirmPassword').val();
	var verifyCode = $('#verifyCode').val();
	var phoneCode = $('#phoneCode').val();
	if(validate()) {
		var params = {
			"userName" : username,
			"password" : password,
			"confirmPassword" : confirmPassword,
			"verifyCode" : validateRules.isMobile(username) ? phoneCode : verifyCode,
			"ReturnUrl" : $('#returnUrl').val(),
			"platform" : $('#platform').val()
		};
		api.localAjaxForJson(api.localUserRegister, params, function getJSON(json) {
			if (json.code != 1) {
				$('#errorTip').removeClass('hide');
				$('#errorTip').html('<p class="padding">'+json.tip.replace("请求处理失败：","")+'</p>');
				img_btn.attr("src", "http://ucenter.ehaoyao.com/showValidateCode.json?" + Math.random());
			} else {
				window.location = json.ReturnUrl;
			}
		}, "post");
	}
});

function validate() {
	var username = $('#username').val();
	var password = $('#password').val();
	var confirmPassword = $('#confirmPassword').val();
	var verifyCode = $('#verifyCode').val();
	var phoneCode = $('#phoneCode').val();
	var flag = false;
	if (validateRules.isNull(username)) {
		showMsg("username","账号不能为空");
		flag = false;
	} else if(validateRules.isMobile(username)) {
		flag = validatePhone(username);
	} else if(validateRules.isEmail(username)) {
		flag = validateEmail(username);
	} else {
		flag = validateName(username);
	}
	if(validateRules.isMobile(username)) {
		if(!validateRules.isNull(password) && !validateRules.isPwd(password)) {
			showMsg("password","密码为6-20位");
			flag = false;
		}
	} else {
		if (!validateRules.isPwd(password)) {
			showMsg("password","密码为6-20位");
			flag = false;
		}
	}
	if(validateRules.isMobile(username)) {
		if(!validateRules.isNull(password) && !validateRules.isPwdRepeat(password, confirmPassword)) {
			showMsg("confirmPassword","两次输入的密码不一致");
			flag = false;
		}
	} else {
		if (!validateRules.isPwdRepeat(password, confirmPassword)) {
			showMsg("confirmPassword","两次输入的密码不一致");
			flag = false;
		}
	}
	if(!validateRules.isMobile(username)) {
		if(validateRules.isNull(verifyCode)) {
			showMsg("verifyCode","验证码不能为空");
			flag = false;
		}
	} else {
		if(validateRules.isNull(phoneCode)) {
			showMsg("phoneCode","手机验证码不能为空");
			flag = false;
		}
	}
	if(isAgree == 0) {
		$('#errorTip').removeClass('hide');
		$('#errorTip').html('<p class="padding">请勾选同意注册协议</p>');
		flag = false;
	}
	return flag;
}

function run(){
	var s = $("#getDypw");
	var sj=s.text();
	if(sj == 0){
		s.removeClass("btn-disabled");
		s.removeAttr("disabled","disabled");
		return false;
	}
	s.text(sj * 1 - 1);
}

function showMsg(id,tip) {
	$('#'+id).parents('li').addClass('alert');
	$('#'+id+'Tip').removeClass('hide');
	$('#'+id+'Tip').html('<p class="padding">'+tip+'</p>');
}

function validatePhone(phone) {
	var params = {'phone':phone};
	var flag = true;
	api.localAjaxForJson(api.localVerifyPhone, params, function(json) {
		if(json) {
			if (json.result == false || json.result == 'false') {
				showMsg("username", "该手机号码已被注册");
				flag = false;
			}
		} else {
			showMsg("username", "服务异常");
			flag = false;
		}
	}, 'get');
	return flag;
}

function validateName(name) {
	if(!validateRules.isUserName(name)) {
		showMsg("username", "账号由2-20位中文,字母,数字或'_'组成");
		return false;
	}
	var flag = true;
	var params = {'name':name};
	api.localAjaxForJson(api.localVerifyName, params, function(json) {
		if(json) {
			if (json.result == false || json.result == 'false') {
				showMsg("username", "该账号已被注册");
				flag = false;
			}
		} else {
			showMsg("username", "服务异常");
			flag = false;
		}
	}, 'get');
	return flag;
}

function validateEmail(email) {
	var params = {'email':email};
	var flag = true;
	api.localAjaxForJson(api.localVerifyEmail, params, function(json) {
		if(json) {
			if (json.result == false || json.result == 'false') {
				showMsg("username", "该邮箱已被注册");
				flag = false;
			}
		} else {
			showMsg("username", "服务异常");
			flag = false;
		}
	}, 'get');
	return flag;
}